const webpack = require('webpack');
const path = require('path');

module.exports = function (chain) {
  chain.module
    .rule('vantui-loader')
    .test(/node_modules[\\/]@antmjs[\\/]vantui[\\/]/i)
    .pre()
    .use('vantui-loader')
    .loader(path.join(process.cwd(), 'config/webpack/vantuiLoader'));

  // 如果不只支持class的宿主环境 需要转es5，钉钉H5就不支持，taro虚拟滚动也需要转es
  // chain.module
  //   .rule('recoil-loader')
  //   .test(/node_modules[\\/]recoil[\\/]/i)
  //   .use('recoil-loader')
  //   .loader(require.resolve('babel-loader'))
  //   .options({});

  if (process.env.NODE_ENV === 'production') {
    chain.merge({
      optimization: {
        minimize: true,
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            'chunk-custom-components': {
              test: /[\\/]node_modules[\\/](dayjs|lodash).*[\\/]/,
              name: 'chunk-custom-components',
              enforce: true,
              priority: 29,
            },
            'chunk-custom-common': {
              test: /[\\/]src[\\/](components).*[\\/]/,
              name: 'chunk-custom-common',
              enforce: true,
              priority: 28,
            },
            'chunk-vantui': {
              test: /[\\/]node_modules[\\/](@antmjs).*[\\/]/,
              name: 'chunk-vantui',
              enforce: true,
              priority: 27,
            },
            'chunk-vant': {
              test: /[\\/]node_modules[\\/](@vant|vant).*[\\/]/,
              name: 'chunk-vant',
              enforce: true,
              priority: 26,
            },
            'chunk-vh-mobile': {
              test: /[\\/]node_modules[\\/](@vh-mobile|vh-mobile).*[\\/]/,
              name: 'chunk-vh-mobile',
              enforce: true,
              priority: 25,
            },
            'chunk-recoil': {
              test: /[\\/]node_modules[\\/](recoil).*[\\/]/,
              name: 'chunk-recoil',
              enforce: true,
              priority: 24,
            },
            'chunk-react-vendor': {
              test: /[\\/]node_modules[\\/](react).*[\\/]/,
              name: 'chunk-react-vendor',
              enforce: true,
              priority: 23,
            },
            'chunk-taro': {
              test: /[\\/]node_modules[\\/](@taro|taro).*[\\/]/,
              name: 'chunk-taro',
              enforce: true,
              priority: 22,
            },
            'chunk-runtime': {
              test: /[\\/]node_modules[\\/](@babel).*[\\/]/,
              name: 'chunk-runtime',
              enforce: true,
              priority: 21,
            },
            'chunk-vendors': {
              test: /[\\/]node_modules[\\/]/,
              name: 'chunk-vendors',
              enforce: true,
              priority: 20,
            },
          },
        },
      },
      //配置额外的引入，不需要打包
      // externals: {
      //   dayjs: 'dayjs',
      //   'ali-oss': 'ali-oss',
      // },
    });
  }

  // chain.plugin('analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, []);
};
